﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ExtensionForIEnumerable
{
    public static class ExtensionForIEnumerable
    {
        public static dynamic Sum<T>(this IEnumerable<T> enumeration) 
        {
            if (enumeration != null)
            {
                dynamic sum = 0;
                foreach (var item in enumeration)
                {
                    sum += item;
                }
                return sum;
            }
            else 
            {
                throw new ArgumentNullException("Colection is empyt");
            }
        }

        public static dynamic Product<T>(this IEnumerable<T> enumeration) 
        {
            if (enumeration != null)
            {
                dynamic product = 1;
                foreach (var item in enumeration)
                {
                    product *= item;
                }
                return product;
            }
            else
            {
                throw new ArgumentNullException("Colection is empyt");
            }
        }

        public static dynamic Min<T>(this IEnumerable<T> enumeration) 
        {
            if (enumeration != null)
            {
                dynamic min = enumeration.First();
                foreach (var item in enumeration)
                {
                    if (item < min)
                    {
                        min = item;
                    }
                }
                return min;
            }
            else 
            {
                throw new ArgumentNullException("Colection is empyt");
            }
        }

        public static dynamic Max<T>(this IEnumerable<T> enumeration)
        {
            if (enumeration!=null)
            {
                dynamic max = enumeration.First();
                foreach (var item in enumeration)
                {
                    if (item > max)
                    {
                        max = item;
                    }
                }
                return max;
            }
            else
            {
                throw new ArgumentNullException("Colection is empyt");
            }
        }

        public static decimal Average<T>(this IEnumerable<T> enumeration) 
        {
            if (enumeration != null)
            {
                dynamic average = 0;
                int counter = 0;
                foreach (var item in enumeration)
                {
                    average += item;
                    counter++;
                }
                return (decimal)average / counter;
            }
            else 
            {
                throw new ArgumentNullException("Colection is empyt");
            }
        }
    }
}
